<template>
  <!--     github issue  https://github.com/SortableJS/vue.draggable.next/issues/140     -->
  <draggable v-model="paperInfo.sortData" animation="250" item-key="id" group="father" @end="dragEnd">
    <template #item="{ element }">
      <div>
        <div class="section" >
          <div class="title">
            <span v-if="element.type === 2">{{ element.content }}</span>
            <span v-else-if="element.type === 4">例{{ element.index }}</span>
            <span v-else-if="element.type === 3">知识点{{ element.index }} {{ element.content }}</span>
            <span v-else-if="element.cssTitle && !(element.type === 4 || element.type === 2)">{{ element.cssTitle }}</span>
            <span v-else-if="element.title && !(element.type === 4 || element.type === 2)">{{ element.title }}</span>
          </div>
          <div class="content">
            <draggable v-model="element.child" class="transition-child" animation="250" item-key="id" group="child"  @end="dragEnd">
              <template #item="childs">
                <div class="item" v-if="childs.element.type === 4">例{{ childs.element.index }}</div>
                <div class="paragraphItem" v-else-if="childs.element.type === 2">{{ childs.element.content }}</div>
              </template>
            </draggable>
          </div>
        </div>
      </div>
    </template>
  </draggable>
</template>

<script lang="ts">
import { computed, Ref } from 'vue';
import draggable from 'vuedraggable';
import store from './../store';
import $, { emitter } from '$';

export default {
  components: { draggable },
  setup() {
    let paperInfo: Ref<any> = computed(() => store.state.paperInfo);
    // 数据平铺
    const tileData = (data) => {
      let arr: any[]= [];
      const expanded = (datas) => {
        if (datas && datas.length > 0){
          datas.map(item => {
            arr.push(item);
            expanded(item.child);
          })
        }
      };
      expanded(data);
      return arr;
    }
    const dragEnd = (e) => {
      let currentQuestList = $.clone(paperInfo.value.sortData)
      currentQuestList = tileData(currentQuestList)
      emitter.emit('change-data', currentQuestList)
      store.commit('set_paper_content', currentQuestList);
    }
    return { dragEnd, paperInfo, tileData }
  }
}
</script>

<style lang="scss" scoped>
.section {
  border-radius: 4px;
  overflow: hidden;
  border: solid 1px transparent;
  background: #fff;
  margin-top: 10px;
  &:hover {
    border-color: #72b22a;
    background: #F5F7FA;
    .title {
      color: #fff;
      background: #72b22a;
      white-space: nowrap;
      text-overflow: ellipsis;
      overflow: hidden
    }
    :deep(.title) .el-dropdown .el-dropdown-selfdefine {
      color: #fff;
    }
  }
  .title {
    padding: 0 10px;
    line-height: 32px;
    max-width: 240px;
    white-space: nowrap;
    text-overflow: ellipsis;
    overflow: hidden
  }
  :deep(.title) .el-dropdown {
    float: right;
    line-height: 30px;
    cursor: pointer;
    .el-dropdown-selfdefine {
      font-size: 12px;
      color: #77808D;
    }
  }
  .content {
    padding: 10px 5px 0px 15px;
    display: flex;
    flex-wrap: wrap;
    .transition-child{
      display: flex;
      flex-wrap: wrap;
      width: 100%;
      min-height: 10px;
    }
    .item {
      margin: 0 15px 10px 0;
      height: 25px;
      padding: 0 8px;
      line-height: 25px;
      border-radius: 4px;
      border: 1px solid #DCDFE6;
      transition: all .25s;
      cursor: pointer;
      max-width: 200px;
      white-space: nowrap;
      text-overflow: ellipsis;
      overflow: hidden;
      &:hover {
        color: #fff;
        background: #72b22a;
        border-color: #72b22a;
      }
    }
    .paragraphItem {
      line-height: 25px;
      border-radius: 4px;
      padding: 0 8px;
      cursor: pointer;
      width: 200px;
      max-width: 200px;
      white-space: nowrap;
      text-overflow: ellipsis;
      overflow: hidden;
      margin: 0 15px 10px 0;
      &:hover {
        color: #fff;
        background: #72b22a;
        border-color: #72b22a;
      }
    }
  }
}
</style>
